﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Universal
{
    public class UserLogin
    {
        /// <summary>
        /// 密钥登陆
        /// </summary>
        /// <param name="strUserID">用户登陆名</param>
        /// <param name="strKeyID">密钥ID</param>
        /// <returns></returns>
        public static bool ProcessLogin(string strUserName, string strPassWord)
        {
            BLL.SysManage.T_PUB_USER userBll = new BLL.SysManage.T_PUB_USER();
            Model.SysManage.T_PUB_USER userModel;
            userModel = userBll.GetModelByUserSymbol(strUserName);

            if (userModel != null && userModel.PASSWORD.Equals(strPassWord) && userModel.ISUSED!=false)
            {
                //判断是否能前台编辑文件属性
                BLL.SysManage.T_PUB_MENU bll = new BLL.SysManage.T_PUB_MENU();
                List<Model.SysManage.T_PUB_MENU> menuList = bll.GetUserMenuList(userModel.USER_ID.ToString());
                HttpContext.Current.Session["FILE_EDIT"] = false;
                for (int i = 0; i < menuList.Count; i++)
                {
                    if (menuList[i].URL == "../Progect/PhotoManage/FileList.aspx" || menuList[i].MENU_ID == 24)
                    {
                        HttpContext.Current.Session["FILE_EDIT"] = true;
                    }
                }
                //if (menuList.Where(p => p.URL.Equals("../Progect/PhotoManage/FileList.aspx")). || menuList.Where(p => p.MENU_ID == 24) != null)
                //{
                //    HttpContext.Current.Session["FILE_EDIT"] = true;
                //}
                //else
                //{
                //    HttpContext.Current.Session["FILE_EDIT"] = false;
                //}
                StringBuilder strl = new StringBuilder("<script language='javascript'>\n");
                //    //得到所在机构角色
                //    NH_ORGANIZATION_BLL OrgBll = new NH_ORGANIZATION_BLL();
                //    string strRole = OrgBll.GetRole(userModel.ORGANIZATIONCODE);

                //保存Session
                //HttpContext.Current.Session["USERNAME"] = strUserName;
                //HttpContext.Current.Session["USERWORD"] = strPassWord;
                HttpContext.Current.Session["USER_ID"] = userModel.USER_ID;
                HttpContext.Current.Session["USER_NAME"] = userModel.NAME;
                HttpContext.Current.Session["BRANCH_ID"] = userModel.BRANCH_ID;
              
                //保存cookie
                HttpCookie cookie = new HttpCookie("EMPInfo");
                cookie.Values.Add("USER_ID", HttpUtility.UrlEncode(Convert.ToString(userModel.USER_ID)));
                cookie.Values.Add("USER_NAME", HttpUtility.UrlEncode(userModel.NAME));

                cookie.Values.Add("BRANCH_ID", HttpUtility.UrlEncode(Convert.ToString(userModel.BRANCH_ID)));
                HttpContext.Current.Response.Cookies.Add(cookie);

                //strl.Append("location.href = \"../View/ViewList.htm\";\n");
                if (HttpContext.Current.Session["pcoripad"].ToString() == "ipad")
                {
                    strl.Append("location.href = \"../View/pList.aspx\";\n");
                }
                else
                {
                    strl.Append("location.href = \"../View/ViewList.aspx\";\n");
                }
                strl.Append("</script>\n");
                HttpContext.Current.Response.Write(strl.ToString());
                return true;
            }

            else
            {
                return false;
            }
        }
    }
}
